package com.jdym.back.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jdym.R;
import com.jdym.back.entity.Firm;
import com.jdym.back.entity.Loan;
import com.jdym.back.entity.Uploadfile;
import com.jdym.back.entity.vo.LoanInfo;
import com.jdym.back.service.FirmService;
import com.jdym.back.service.LoanService;
import com.jdym.back.service.UploadfileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 企业表 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2021-09-20
 */
@Api("企业用户管理")
@CrossOrigin
@RestController
@RequestMapping("/back/firm")
public class FirmController {
    @Autowired
    FirmService firmService;
    @Autowired
    LoanService loanService;
    @Autowired
    UploadfileService uploadfileService;

//    获取所有贷款的企业
    @GetMapping("/getFirmByKeyWord")
    public R getFirm(@RequestParam String keyWord1,@RequestParam String keyWord2){
      List<Firm> firms=  firmService.getBaseMapper().selectList(new QueryWrapper<Firm>().like("firm_name",keyWord1).like("firm_tel",keyWord2));
        return R.ok().data("firm",firms);
    }

    @ApiOperation("获取所有贷款企业")
    @GetMapping("/getAllLoanFirm")
    public R getAllLoanFirm(){


       List<Firm> firms= firmService.getAllLoanFirm();

       return R.ok().data("firms",firms).message("成功获取所有贷款公司");
    }
//冲突
//    @ApiOperation("通过id获取贷款企业的详细信息")
//    @GetMapping("/detail/{firmId}")
//    public R getLoanDetailByFirmId(@PathVariable int firmId){
//        Map<String,Object> detail=new HashMap<>();
////        获取贷款公司信息
//        List<Loan> loan=loanService.getLoanById(firmId);
////        获取贷款公司
//        Firm firm=firmService.getFirmById(firmId);
////        获取贷款公司上传信息
////        Uploadfile uploadfile=uploadfileService.getUpFile(firmId);
//        detail.put("firm",firm);
//        detail.put("loan",loan);
////        detail.put("uploadfile",uploadfile);
//        return R.ok().data("detail",detail).message("获取成功");
//    }
//业务员查看所有申请贷款企业
    @RequestMapping("/allLoanFirm")
    public R allLoanFirm(){
        List<Firm> firms=firmService.getBaseMapper().selectList(null);
        return R.ok().data("firms",firms);
    }
//    查看历史申请贷款
    @RequestMapping("/historyLoan/{firmId}")
    public R historyLoan(@PathVariable int firmId){
        List<LoanInfo> loanInfos=loanService.historyLoan(firmId);
        return R.ok().data("loanInfo",loanInfos);
    }
//    查找所有的企业用户
    @RequestMapping(value = "/findAllFirmUser")
    public R findAllFirmUser(){
        List<Firm> firms=firmService.getBaseMapper().selectList(null);
        return R.ok().data("firms",firms);
    }
//    根据企业id查找企业
    @RequestMapping(value = "/findAllFirmUserById/{firmId}")
    public R findAllFirmUser(@PathVariable int firmId){
        Firm firms=firmService.getBaseMapper().selectById(firmId);
        return R.ok().data("firms",firms);
    }
//    更新企业用户
    @RequestMapping(value = "/updateFrimUser")
    public R updateFrimUser(int firmId,String firmName,String firmTel,String legalperson,String firmEmail){
        Firm firm=new Firm();
        firm.setFirmId(firmId);
        firm.setFirmName(firmName);
        firm.setFirmTel(firmTel);
        firm.setLegalperson(legalperson);
        firm.setFirmEmail(firmEmail);
        firmService.getBaseMapper().updateById(firm);
        return R.ok();
    }
//    删除企业
    @DeleteMapping(value = "/delFirmById/{firmId}")
    public R delFirmById(@PathVariable int firmId){
        firmService.getBaseMapper().deleteById(firmId);
        return R.ok();
    }

//    查询所有企业
    @GetMapping(value = "/getAllFirms")
    public R getAllFirms(){
        List<Firm> firms=firmService.getBaseMapper().selectList(null);
        return R.ok().data("firms",firms);
    }
    //////////////////////////////张



    @ApiOperation("通过id获取贷款企业的详细信息")
    @GetMapping("/detail/{firmId}")
    public R getLoanDetailByFirmId(@PathVariable int firmId){
        Map<String,Object> detail=new HashMap<>();
//        获取贷款公司信息
        Loan loan=loanService.getLoanById(firmId);
//        获取贷款公司
        Firm firm=firmService.getFirmById(firmId);
//        获取贷款公司上传信息
        Uploadfile uploadfile=uploadfileService.getUpFile(firmId);
        detail.put("firm",firm);
        detail.put("loan",loan);
        detail.put("uploadfile",uploadfile);
        return R.ok().data("detail",detail).message("获取成功");
    }

    //企业用户和贷款表的一对一的查询
    @ApiOperation("获取企业贷款信息")
    @GetMapping("/getLoanFirm")
    public R getFirm(@RequestParam("key") String key){
        List<Firm> firms = firmService.getLoanFirm(key);
        for (Firm firm:firms){
            System.out.println(firm.getFirmName()+
                    firm.getLegalperson()+firm.getLoan().getLoanAmount());
        }
        System.out.println("=============="+firms);
        return R.ok().data("list",firms).message("获取成功");
    }

}

